import { ref, reactive, computed } from "vue";
import { defineStore } from "pinia";
import { DCMStudy, DCMSeries, DCMImage } from "@/pacs";
import cornerstoneDICOMImageLoader from "@cornerstonejs/dicom-image-loader";

export const useArchiveStore = defineStore("archive", () => {
	let archiveData = reactive({
		studyList: [],
		mainStudyId: "",
		orgId: ""
	});

	const currentStudy = ref(null);
	const seriesList = computed(() => {
		return currentStudy.value ? currentStudy.value.seriesList : [];
	});

	function archiveObj(obj) {}

	function sort(bAsc = true) {
		archiveData.studyList.sort(function (a, b) {
			if (bAsc) return a.studyDate.localeCompare(b.studyDate);
			else return b.studyDate.localeCompare(a.studyDate);
		});

		archiveData.studyList.forEach(study => {
			study.Sort(bAsc);
			study.seriesList.forEach(series => {
				series.Sort(bAsc);
			});
		});
	}

	function getCount() {
		return archiveData.studyList.length;
	}

	function setCurrentStudy(study) {
		currentStudy.value = study;
	}

	async function archiveFile(dcm) {
		// console.log("archiveFile, file: ", dcm);
		let imageId = "";
		if (typeof dcm === "string") {
			// 	await cornerstoneDICOMImageLoader.wadouri.loadImage(dcm)
			//  .promise;
			imageId = dcm;
		} else {
			imageId = cornerstoneDICOMImageLoader.wadouri.fileManager.add(dcm);
		}

		const dcmImage = new DCMImage({ imageId });

		await dcmImage.parse();
		// console.log("archiveFile, dcmImage: ", dcmImage);

		const dstObj = archiveData;
		if (!dstObj.mainStudyId) {
			dstObj.dataformat = "StudyList";
			dstObj.mainStudyId = dcmImage.PatientID;
			dstObj.orgId = "";
			dstObj.studyList = [];
		} else {
			// dstObj.dataformat = archiveData.dataformat;
			// dstObj.mainStudyId = archiveData.mainStudyId;
			// dstObj.orgId = archiveData.orgId;
			// dstObj.studyList = archiveData.studyList;
		}

		const study = dstObj.studyList.find(s => s.studyInsUid === dcmImage.studyInsUid);

		if (study) {
			const series = study.seriesList.find(
				s => s.seriesInsUid === dcmImage.seriesInsUid
			);

			if (series) {
				series.AddImage(dcmImage);
			} else {
				// Add Series
				const series = new DCMSeries({
					study,
					seriesInsUid: dcmImage.seriesInsUid,
					seriesNumber: dcmImage.SeriesNo.split(":")[1],
					seriesDesc: dcmImage.SeriesDesc,
					seriesDate: dcmImage.SeriesDateTime.split(" ")[0],
					seriesTime: dcmImage.SeriesDateTime.split(" ")[1],
					radiId: dcmImage.RadiID
				});
				series.AddImage(dcmImage);
				study.AddSeries(series);
			}
		} else {
			// Add Study
			const study = new DCMStudy({
				patientId: dcmImage.PatientID,
				patientName: dcmImage.PatientName,
				studyAge: dcmImage.AgeAndSex.split(" ")[0],
				birthday: dcmImage.AgeAndSex.split(" ")[0],
				gender: dcmImage.AgeAndSex.split(" ")[1],
				studyId: dcmImage.PatientID,
				studyInsUid: dcmImage.studyInsUid,
				radiId: dcmImage.RadiID,
				studyDate: dcmImage.StudyDateTime,
				modality: dcmImage.DeviceName,
				isMain: dcmImage.PatientID === dstObj.mainStudyId
			});

			// Add Series
			const series = new DCMSeries({
				study,
				seriesNumber: dcmImage.SeriesNo.split(":")[1],
				seriesInsUid: dcmImage.seriesInsUid,
				seriesDesc: dcmImage.SeriesDesc,
				seriesDate: dcmImage.SeriesDateTime.split(" ")[0],
				seriesTime: dcmImage.SeriesDateTime.split(" ")[1]
			});

			series.AddImage(dcmImage);
			study.AddSeries(series);
			dstObj.studyList.push(study);
			// console.log(
			// 	"New Study New Series, stuNum:" +
			// 		dstObj.studyList.length +
			// 		", serNumb:" +
			// 		study.seriesList.length
			// );
		}
	}

	return {
		archiveData,
		seriesList,
		currentStudy,
		getCount,
		sort,
		archiveObj,
		setCurrentStudy,
		archiveFile
	};
});
